<!-- 商店子页面 -->
<template>
  <div class="shell" v-if="$store.state.gameLoad||false">
    <!-- 顶部栏 -->
    <div class="top">
      <div class="navBar">
        <div class="wish">
          <!-- 购物车页面跳转按钮 -->
          <button @click="$router.push(`/home/cart?id=3`);">购物车</button>
        </div>
        <!-- 展示条 -->
        <div class="bar">
          <div class="items">
            <div class="item">
              <span>欢迎您的到来！</span>
            </div>
          </div>
          <!-- 搜索框，可以搜索游戏 -->
          <div class="search" @keyup.enter="searchGame(toGame)">
            <input
              placeholder="搜索"
              type="text"
              v-model="toGame"
              spellcheck="false"
            />
            <div class="icon" @click="searchGame(toGame)">
              <img
                src="https://store.cloudflare.steamstatic.com/public/images/v6/search_icon_btn_over.png"
                alt=""
              />
            </div>
          </div>
        </div>
      </div>
      <!-- 背景图片 -->
      <div class="banner">
        <img src="@/assets/page_bg_schinese.jpg" alt="" />
      </div>
    </div>
    <!-- 中部游戏展示区 -->
    <div class="middle">
      <!-- 轮播图展示热门游戏 -->
      <el-carousel
        ref="carousel"
        trigger="click"
        height="400px"
        indicator-position="outside" 
        :autoplay = "carStatu&&!$store.state.videoPlay"
        @mouseenter="carStatu=false"
        @mouseleave="carStatu=true"
        @change="handleCarouselChange"
        :interval="4000"
      >
        <el-carousel-item v-for="(v,i) in $store.state.allGame.slice(0,5)" :key="i">
          <!-- 热门游戏组件，根据数据动态生成 -->
          <hotGameCardVue ref="hotGameCards" :game="v" :num="i"></hotGameCardVue>
        </el-carousel-item>
      </el-carousel>
      <!-- 刷新按钮 -->
      <div class="refresh-btn-container">
        <el-button 
          class="refresh-btn" 
          @click="handleRefresh"
          :loading="refreshing"
        >
          <el-icon><Refresh /></el-icon>
          <span>刷新</span>
        </el-button>
      </div>
      <!-- 普通游戏容器 -->
      <div class="games" ref="game">
        
        <div class="game-list">
          <div class="t-list">
            <!-- 普通游戏卡片组件，展示普通游戏 -->
            <gameCardVue ref="norGame" v-for="(v,i) in $store.state.allGame.slice((curPage-1)*9+5,(curPage)*9+5)" :key="i" :num = "i+5+(curPage-1)*9" :game="v"></gameCardVue>
          </div>
          <div class="pages">
            <!-- 翻页器控制页数，一页展示9个游戏 -->
            <el-pagination background small layout="prev, pager, next"  :page-size="9" v-model:current-page="curPage" :total="$store.state.allGame.length-5" />
          </div>
        </div>
        <div class="pic-show">
          <!-- 与普通游戏卡片组件配合的图片展示容器，展示对应游戏的内容 -->
          <showGamePicsVue :game="$store.state.allGame[$store.state.shop.hoverGame]"></showGamePicsVue>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 引入组件
import { ElLoading, ElIcon } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import hotGameCardVue from '@/components/hotGameCard.vue';
import gameCardVue from '@/components/gameCard.vue';
import showGamePicsVue from '@/components/showGamePics.vue';
export default {
  data() {
    return {
      toGame: "",
      loading: false,
      carStatu: true,
      curPage: 1,
      refreshing: false
    };
  },
  methods: {
    // 处理轮播切换事件
    handleCarouselChange(currentIndex, previousIndex) {
      // 暂停之前的视频
      if (this.$refs.hotGameCards && this.$refs.hotGameCards[previousIndex]) {
        this.$refs.hotGameCards[previousIndex].pauseVideo();
      }
      // 更新视频播放状态
      this.$store.commit('changeVideoPlay', false);
    },
    // 搜索游戏
    async searchGame(name) {
      // 节流
      if(this.loading) return ;
      this.loading = true;
      // 保存外部作用域
      const that = this;
      const query = {gname: name};
      const loadOptions = {text: '加载中',background: 'rgba(23,29,37,0.7)'};
      // 加载
      const loadingInstance = ElLoading.service(loadOptions)
      await that.$axios.get("goods/searchGames", {params:query}).then((response) => {
          console.log(response);
          if (response.status != 200||!response.data.code) return that.$tools.alertMsg("搜索失败",'error');
          if (response.data.data.length != 0) {
            // that.findPosition(response.data.data[0].gname)
            that.$store.commit('saveGames',{gameList: response.data.data,flag:name});
            that.findPosition();
            that.$tools.alertMsg('成功找到游戏','success');
          } else {
            that.$tools.alertMsg('游戏不存在','warning');
          }
        })
        .catch(function (error) {
          console.log(error);
        });
      loadingInstance.close();
      this.loading = false;
    },
    async getGames(){
      const that = this;
      await that.$axios.get("goods/findAll").then((response) => {
          console.log("getGames",response);
          if (response.status != 200) return that.$tools.alertMsg('获取数据失败，系统错误','error');
          if (response.data.data.length != 0) {
            that.$store.commit('saveGames',{gameList: response.data.data,flag:''});
          } else {
            that.$tools.alertMsg('获取游戏数据失败','error'); 
          }
      }).catch(function (error) {
          console.log(error);
      });
    },
    // 刷新按钮点击事件
    async handleRefresh() {
      this.refreshing = true;
      try {
        await this.getGames();
        this.$tools.alertMsg('刷新成功', 'success');
      } catch (error) {
        console.error(error);
        this.$tools.alertMsg('刷新失败', 'error');
      } finally {
        this.refreshing = false;
      }
    },
    // 滚动到游戏的位置并且显示出来
    async findPosition(){
      this.$refs['norGame'][0].$el.scrollIntoView({ behavior: 'smooth' });
    }
  },
  async created(){
    await this.getGames();
  },
  components:{
    hotGameCardVue,
    gameCardVue,
    showGamePicsVue,
    ElIcon,
    Refresh
  },
};
</script>

<style lang="less" scoped>
.shell {
  height: calc(100vh - 72px);
  // 顶部样式
  .top {
    height: 440px;
    position: relative;
    display: flex;
    justify-content: center;
    // background: #0A084F;
    .navBar {
      position: absolute;
      display: flex;
      flex-direction: column;
      top: 0px;
      z-index: 1;
      .wish {
        margin-top: 8px;
        margin-bottom: 6px;
        text-align: right;
        button {
          width: 100px;
          padding: 2px 6px 4px 6px;
          border-radius: 2px;
          font-size: 12px;
          color: #fff;
          background-color: #6e96a2;
        }
      }
      .bar {
        background: linear-gradient(to right, #396594, #0f216e);
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 920px;
        margin: auto;
        .items {
          display: flex;
          height: 36px;
          line-height: 40px;
          .item {
            color: #fff;
            font-size: 13px;
            font-weight: bold;
            padding: 0px 16px;
            cursor: pointer;
            &:hover {
              background: linear-gradient(to right, #307ab9, #396191);
            }
          }
        }
        .search {
          display: flex;
          align-items: center;
          background-color: #316282;
          border-radius: 4px;
          padding: 0px 2px;
          input {
            height: 30px;
            color: #fff;
            background-color: #316282;
            text-indent: 14px;
            padding-right: 8px;
            border-radius: 4px;
            &::placeholder {
              color: #aaa;
              font-style: italic;
            }
          }
          img {
            vertical-align: middle;
            height: 26px;
            width: 26px;
            border-radius: 4px;
            cursor: pointer;
          }
        }
      }
    }
    .banner {
      position: relative;
      width: 100%;
      img {
        object-fit: cover;
        width: 100%;
        height: 440px;
      }
    }
  }
  // 中部样式
  .middle{
    padding: 10px 0px;
    max-width: 960px;
    margin: 0px auto;
    .refresh-btn-container {
      display: flex;
      justify-content: flex-start;
      margin: 10px 0 0 0;
      .refresh-btn {
        background: linear-gradient(to right, #396594, #304bc5);
        color: #fff;
        border: none;
        padding: 8px 16px;
        border-radius: 4px;
        font-size: 14px;
        display: flex;
        align-items: center;
        transition: all 0.3s;
        &:hover {
          background: linear-gradient(to right, #307ab9, #396191);
          transform: translateY(-2px);
        }
        .el-icon {
          margin-right: 6px;
        }
      }
    }
    .games{
      height: 730px;
      width: 100%;
      // margin: 20px 0px;
      padding: 10px 0px 20px 0px;
      display: flex;
      .game-list{
        width: 66%;
        height: 730px;
        background-color: #284258;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .t-list{
          display: flex;
          flex-direction: column;
        }
        .pages{
          height: 38px;
          background: linear-gradient(to right,#1A2A3C,#A6CAE0);
          // background: linear-gradient(to bottom,#284258,#1A2A3C);
          display: flex;
          align-items: center;
          justify-content: center;
        }
      }
      .pic-show{
        width: 34%;
        height: 730px;
        background: linear-gradient(to right,#A6CAE0,#859BAC);
        
      }
    }
  }
}
</style>